import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:im_base/private/app_ext.dart';
import 'package:im_base/public/components/custom_picker_delegate.dart';
import 'package:im_base/public/ext/picker_ext.dart';
import 'package:im_base/public/global/global.dart';
import 'package:im_base/public/service/tim_service.dart';
import 'package:im_base/public/tim/components/chat/input/logic.dart';
import 'package:im_base/public/tim/model/menu_data.dart';
import 'package:image_picker/image_picker.dart';
import 'package:video_thumbnail/video_thumbnail.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_assets_picker/src/constants/constants.dart';
import '../../../../../im_base.dart';
import '../logic.dart';
import 'state.dart';
import 'package:provider/provider.dart';

class MenusLogic extends GetxController {
  final state = MenusState();
  ImagePicker? _picker;
  late ChatLogic chatLogic;
  late InputLogic inputLogic;

  @override
  void onReady() {
    _picker = ImagePicker();
    chatLogic = Get.find<ChatLogic>(tag: state.id);
    inputLogic = Get.find<InputLogic>(tag: state.id);
    super.onReady();
  }

  ///菜单操作逻辑
  void operateMenu(MenuType type) {
    switch (type) {
      case MenuType.TAKE_FILE:
        break;
      case MenuType.TAKE_VIDEO:
        sendVideo();
        break;
      case MenuType.TAKE_PHOTO:
      case MenuType.PHOTO_ALBUM:
        sendImage(type);
        break;
    }
  }

  ///发送图片消息
  void sendImage(MenuType type) async {
    var path;
    if (type == MenuType.PHOTO_ALBUM) {
      var result = await selectPhoto(maxNum: 1);
      if (result == null || result.isEmpty) {
        return;
      }
      var image = await result.first.originFile;
      path = image?.path;
    } else {
      var result = await _picker?.pickImage(source: ImageSource.camera);
      if(result==null){
        return;
      }
      path = result.path;
    }
    inputLogic.showMore(show: false);
    TimService.instance.sendImageMessage(
        path ?? '', chatLogic.state.id ?? "", chatLogic.state.isGroup,
        callBack: (result) {
      if (result.code == 0) {
        chatLogic.updateImageAudioMessage(result.data!);
      } else {
        showToast("消息发送失败[${result.code}]|${result.desc}");
      }
    });
  }

  ///发送视频消息
  void sendVideo() async {
    final video = await selectVideo();
    if (video == null || video.isEmpty) {
      return;
    }
    var videoFile = await video.first.originFile;
    var snapshot =
        await VideoThumbnail.thumbnailFile(video: videoFile?.path ?? '');
    inputLogic.showMore(show: false);
    int duration = video.first.duration;

    TimService.instance.sendVideoMessage(
        videoFile?.path ?? '',
        chatLogic.state.id ?? "",
        chatLogic.state.isGroup,
        duration,
        'mp4',
        snapshot ?? '', callBack: (result) {
      if (result.code == 0) {
        chatLogic.updateImageAudioMessage(result.data!);
      } else {
        showToast("消息发送失败[${result.code}]|${result.desc}");
      }
    });
  }
}
